#include"Stack.h"

void StackInit(Stack* s)
{
	assert(s);
	s->data = NULL;
	s->capacity = s->size = 0;
}

void StackDestory(Stack* s)
{
	assert(s);
	free(s->data);
	s->data = NULL;
	s->capacity = s->size = 0;
}

bool StackEmpty(Stack* s)
{
	return s->size == 0;
}

bool StackFull(Stack* s)
{
	assert(s);
	return s->size == s->capacity;
}

void StackPush(Stack* s, SData x)
{
	assert(s);
	if (StackFull(s))
	{
		int newcapacity = s->capacity == 0 ? 4 : s->capacity * 2;
		SData* temp = (SData*)realloc(s->data,sizeof(SData) * newcapacity);
		if(temp==NULL)
		{
			perror("malloc failed");
			exit(-1);
		}
		s->data = temp;
		s->capacity = newcapacity;
	}
	s->data[s->size++] = x;
}

void StackPop(Stack* s)
{
	assert(s);
	assert(s->size != 0);
	s->size--;
}

SData StackTop(Stack* s)
{
	return s->data[s->size - 1];
}